/*
 * CS 567 GUI - Semester Project - "DecisionLists"
 * Jason Frank
 * Spring 2011
 *
 * This File:  Implements the Butterfly View.
 */

package Decision_Lists;

import Decision_Lists.List_Item_Data.ProCon;
import javax.swing.*;
import java.awt.*;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.lang.reflect.Array;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;
import java.awt.Font;
import javax.swing.border.EtchedBorder;
import javax.swing.JLabel;

/*
 */
public class ButterflyView extends JPanel implements DL_ViewInterface {
//public class ButterflyView extends Panel_Fader_User implements DL_ViewInterface {

  ////////////////////////////////////////////////
  ///// Class Vars ///////////////////////////////
  protected boolean is_center_view = true;
  protected DL_Model model;
  protected TreeMap <String, List_Item_Data> items; // ptr for model's map of items
  protected List_Item_Data currentItem;
  protected final String view_name = "Butterfly";
  protected int panelWidthPref  = 825;
  protected int panelHeightPref = 575;
  protected Color panel_BG_color = new Color(240,240,240); // nearly white
  // Containers
  JPanel mainPanel = new JPanel();  // The panel of the main butterfly (not Prev/Next panel, etc)
  JPanel prevNextPanel = new JPanel(); // Make a panel for Prev & Next btns
  Box mainBox = Box.createHorizontalBox(); // Holds the main vertical columns of the butterfly
  Box prosVertBox = Box.createVerticalBox();
  Box consVertBox = Box.createVerticalBox();
  Box centerVertBox = Box.createVerticalBox();

  protected Butterfly_Center centerWidget;
  protected int centerWidgetWidth  = 330;
  protected int centerWidgetHeight = 310;
      //int centerWidgetHeight = 240;

  List <ProConPanel> proPanels = new LinkedList<ProConPanel>();
  List <ProConPanel> conPanels = new LinkedList<ProConPanel>();

  protected JLabel proLabel = new JLabel();
  protected JLabel conLabel = new JLabel();
  protected Font proLabelFont = new Font("Helvetica", Font.BOLD,  19);
  protected JPanel proLabelPanel = new JPanel();// For putting the proLabel into: addint the label
                                                // to a panel makes better layout amongst other panels
  protected JPanel conLabelPanel = new JPanel();
  protected Color prosLabelColor = new Color(80, 200, 70);  // R,G,B. So its Green-ish.
  protected Color consLabelColor = new Color(200, 70, 60);  // R,G,B. So its Red-ish.

  protected int centerPanelShiftDown;  // # pixels to shift it down to account for "Pros/Cons" labels.
  protected final int consColumnTopPad = 16;
  protected final int prosColumnTopPad = 16;

  protected int lineXCenterLeft;
  protected int lineXCenterRight;
  protected int lineYCenter;
  protected BasicStroke lineStroke = new BasicStroke(3);
  protected Color lineColor = new Color(120,120,120); // gray

  // Prev/Next links
  protected JButton prevItemBtn = new JButton("Prev Item");
  protected JButton nextItemBtn = new JButton("Next Item");

  protected mainButterflyPanel current_main_panel;
  //// end Class vars /////
  /////////////////////////

  /////////////////////
  // Constructor
  public ButterflyView(DL_Model model) {
    
    this.model = model;           // link the model to this view
    this.model.addObserver(this, this.view_name, is_center_view); // link this view to the model
    this.items = model.getItemsMap();
    // Set the current item to be the first item in the list to start with
    currentItem = items.firstEntry().getValue();

    // TODO: figure out if I need an ARRAY of list items in the model
    //        (so I can move to next item, etc), along with the treemap
    //currentItem = ((Array)items)[2];

    //current_main_panel = new mainButterflyPanel(currentItem);



    initVisibleComponents();

    layoutEntireView();

    //super.init_PanelFaderUser(mainPanel, current_main_panel);

            //System.err.println("End of constructor in ButterflyView.");
            //System.err.println(" in ButterflyView, Butterfly_Center's panelHeightPref: " +
            //                                     centerWidget.panelWidthPref);
  } ////////// end constructor /////////////

  // Initializes (and instantiates for some) visible components in prep of being layed-out
  protected void initVisibleComponents() {
    // <editor-fold defaultstate="collapsed" desc="method contents...">
    // Prev/Next Btns: set them up to look like JLabels (http://stackoverflow.com/questions/3025320/draw-a-jbutton-to-look-like-a-jlabel-or-at-least-without-the-button-edge)
    prevItemBtn.setMargin(new Insets(0, 0, 0, 0));
    prevItemBtn.setContentAreaFilled(false);
    prevItemBtn.setBorderPainted(false);
    prevItemBtn.setOpaque(false);
    nextItemBtn.setMargin(new Insets(0, 0, 0, 0));
    nextItemBtn.setContentAreaFilled(false);
    nextItemBtn.setBorderPainted(false);
    nextItemBtn.setOpaque(false);

    // Instantiate the Center Panel
    //centerWidget = new Butterfly_Center(model, centerWidgetWidth, centerWidgetHeight, panel_BG_color);
    centerWidget = new Butterfly_Center(currentItem, centerWidgetWidth, centerWidgetHeight,
            panel_BG_color);

    // Pro/Con Labels: Set the font and other properties
    proLabel.setFont(proLabelFont);
    proLabel.setText("<html><u>Pros</u></html>"); // add underlining.
    proLabelPanel.add(proLabel);  // add the label to a panel for better layout amongst other panels
    conLabel.setFont(proLabelFont); // reusing pro label font
    conLabel.setText("<html><u>Cons</u></html>");
    conLabelPanel.add(conLabel);
    proLabel.setForeground(prosLabelColor);
    conLabel.setForeground(consLabelColor);
    centerPanelShiftDown = getFontMetrics(proLabelFont).getHeight() + prosColumnTopPad;

    // Instantiate all the PRO and CON PANELS for this list item
    for (ProCon pro : currentItem.getPros()) {
      ProConPanel proPanel = new ProConPanel(pro, panel_BG_color);
      proPanels.add(proPanel);
    }
    for (ProCon con : currentItem.getCons()) {
      ProConPanel conPanel = new ProConPanel(con, panel_BG_color);
      conPanels.add(conPanel);
    }

    setBackground(panel_BG_color);// </editor-fold>
  } ///////// end initVisibleComponents() /////


  // layoutEntireView()
  protected void layoutEntireView() {
    //////////////////////////////////////////////// // <editor-fold defaultstate="collapsed" desc="method contents...">
    //// LAYOUT and visual stuff ///////////////////

    // Setup the panel for Prev & Next btns
    prevNextPanel.add(prevItemBtn);
    prevNextPanel.add(nextItemBtn);
    prevNextPanel.setSize(62, 200);
    prevNextPanel.setMaximumSize(new Dimension(62, 200));
    prevNextPanel.setPreferredSize(new Dimension(62, 200));

    // Make main panel non-opaque so that the drawn lines show through
    mainPanel.setOpaque(false);

    //setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    // BorderLayout so the Prev/Next btns can be placed to the side, etc.
    setLayout(new BorderLayout());

    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    //// LAYOUT MAIN HORIZ BOX ////
    mainBox.add(Box.createHorizontalGlue());  // Left SIDE padding
    ////  ADD COLUMN OF CONS ////
    // First add the "Cons" Label
    consVertBox.add(Box.createRigidArea(new Dimension(0, consColumnTopPad)));
    //consVertBox.add(Box.createVerticalGlue());
    consVertBox.add(conLabelPanel);
    // Next add all the cons panels
    for (ProConPanel conPanel : conPanels) {
      consVertBox.add(Box.createVerticalGlue());
      consVertBox.add(conPanel);
    }
    consVertBox.add(Box.createVerticalGlue());
    // Now add the entire column to the mainBox
    mainBox.add(consVertBox);

    //// Add CENTER PANEL /////
    mainBox.add(Box.createHorizontalGlue());  // Left SIDE of center panel padding
    //centerVertBox.add(Box.createRigidArea(new Dimension(0, centerPanelShiftDown)));
    //centerVertBox.add(Box.createRigidArea(new Dimension(0, 50)));
    centerVertBox.add(Box.createVerticalGlue());  // Top of Center Panel padding
    centerVertBox.add(Box.createVerticalGlue());
    centerVertBox.add(Box.createVerticalGlue());
    centerVertBox.add(centerWidget);
    centerVertBox.add(Box.createVerticalGlue());  // Bottom of Center Panel padding
    centerVertBox.add(Box.createVerticalGlue());
    mainBox.add(centerVertBox);
    mainBox.add(Box.createHorizontalGlue()); // Right SIDE of center panel padding

    ////  ADD COLUMN OF PROS ////
    // First add the "Pros" Label
    prosVertBox.add(Box.createRigidArea(new Dimension(0, prosColumnTopPad)));
    prosVertBox.add(proLabelPanel);
    // Next add all the pros panels
    for (ProConPanel proConPanel : proPanels) {
      prosVertBox.add(Box.createVerticalGlue());
      prosVertBox.add(proConPanel);
    }
    prosVertBox.add(Box.createVerticalGlue());
    // Now add the entire column to the mainBox
    mainBox.add(prosVertBox);


    mainBox.add(Box.createHorizontalGlue()); // Right SIDE padding

    mainPanel.add(Box.createVerticalGlue());  // Used to vertically center the mainBox
    mainPanel.add(mainBox);
    mainPanel.add(Box.createVerticalGlue());

    add(mainPanel, BorderLayout.CENTER);
    // Put Prev & Next Btns on far right
    add(prevNextPanel, BorderLayout.LINE_END);// </editor-fold>
  } /////////// end layoutEntireView() ////////


  // Draws the Lines that connect the center widget to the pro/con panels
  @Override
  public void paintComponent(Graphics g) {
    // Call the superclass paintComponent() to clear the clipping region (avoids ghosting).
    super.paintComponent(g);
    // make a deep copy of the Graphics object before modifying it
    Graphics myG = g.create();
    Graphics2D g2 = (Graphics2D) myG;
    setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

    //////////////////////////////
    //// DRAW LINES //////////////
    initDrawVars();
    g2.setStroke(lineStroke);
    g2.setColor(lineColor);
    int conXEnd = mainPanel.getWidth() / 4;
    // Cons lines
    for(int i = 1; i <= conPanels.size(); i++)  {
      g2.drawLine(lineXCenterLeft, lineYCenter,
                  conXEnd, (((mainPanel.getHeight() / (conPanels.size() +1)) + 0) * i ));
    }

    int proXEnd = mainPanel.getWidth() * 3/4;
    // Pros lines
    for(int i = 1; i <= proPanels.size(); i++)  {
      g2.drawLine(lineXCenterRight, lineYCenter,
                  proXEnd, (((mainPanel.getHeight() / (proPanels.size() +1)) + 0) * i ));
    }

    // clear up resources
    myG.dispose();
  } ///// end paintComponent() ///

  private void initDrawVars() {
    lineXCenterLeft  = mainPanel.getWidth() / 2;
    lineXCenterRight = mainPanel.getWidth() / 2;
    lineYCenter      = (int) ((int) (mainPanel.getHeight() / 2)); // + ((double) mainPanel.getHeight() * .1));

  } /////// end initDrawVars() ////////

  ////////////////////////////////////////////////////
  /// NESTED CLASS:  mainButterflyPanel - the main panel - not the Next/Prev stuff on right
  protected class mainButterflyPanel extends JPanel {




    
  }
  ///// end Nested Class  ////////////
  ////////////////////////////////////


  /////////////////////////////////////////////////
  ////// Methods Required by the Interface ////////

  // the Model calls this to notify this View of a changed current value
  public void curViewChanged(String old_view_name, String new_view_name) {
    if( old_view_name.equals(this.view_name) ) {
      // TODO: change this
      repaint();  // zero-arg verion of repaint() repaints entire contentPane

      // TODO: fade out? (or will a container class do it?)
      
    }
    if( new_view_name.equals(this.view_name)) {

      // TODO: fade in? (or will a container class do it?)
    }
    
  } ////// end curViewChanged() /////////

  // Model calls this to inform views of an added (new) list item.
  //  The views will then probably need to re-layout their list items to reflect the change.
  public void itemAdded(List_Item_Data newItem) {
    // Do nothing
  }
  // Model calls this to inform views of a changed list item (i.e., new item added, old item deleted, etc).
  //  The views will then probably need to re-layout their list items to reflect the change.
  public void itemChanged() {
    // Do nothing
  }
  public String getViewName() {
    return this.view_name;
  } ////

  /////// end required by interface functions  //////
  ///////////////////////////////////////////////////



  @Override
  public Dimension getMinimumSize()   { return getPreferredSize(); }
  @Override
  public Dimension getPreferredSize() { return new Dimension(panelWidthPref, panelHeightPref); }
  
} ////////// end class ButterflyView //////////////////////////
